'use strict';

var moment = require('moment');

module.exports = app => {
  const {
    INTEGER,
    STRING,
    DATE,
  } = app.Sequelize;

  const Comment = app.model.define('comments', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    rid: { // 回复的评论id，如果为-1代表直接评论的帖子
      type: INTEGER,
      defaultValue: -1
    },
    puid: { // 提交评论的uid
      type: INTEGER
    },
    ruid: { // 被评论方的uid
      type: INTEGER
    },
    tid: { // 评论隶属的帖子id
      type: INTEGER
    },

    comment: {
      type: STRING
    },
    state: {
      type: INTEGER,
      defaultValue: 1
    },
    
    created_at: {
      type: DATE,
      get() {
        return moment(this.getDataValue('created_at')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    updated_at:  {
      type: DATE,
      get() {
        return moment(this.getDataValue('updated_at')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
  });

  Comment.associate = function() {
    app.model.Comment.belongsTo(app.model.User, {
      foreignKey: 'puid',
      as: 'puser'
    })
    app.model.Comment.belongsTo(app.model.User, {
      foreignKey: 'ruid',
      as: 'ruser'
    })
  };

  return Comment;
};